<template>
  <div class="container">
      <!-- 顶部区域 -->
      <div class="info-box">
        <van-image
          fit="cover"
          round
          width="64"
          height="64"
          :src="userInfo.photo"
        />
        <h2 class="name">{{userInfo.name}}
        <br>
        <van-tag type="primary" color="#fff" text-color="#3296fa" >{{userInfo.birthday}}</van-tag>
        </h2>
      </div>

      <!-- 操作导航 -->
          <van-row class="user-nav">
            <van-col span="8">
              <van-icon name="newspaper-o" color="#7af" />我的作品
            </van-col>
            <van-col span="8">
              <van-icon name="star-o" color="#f00" />我的收藏
            </van-col>
            <van-col span="8">
              <van-icon name="tosend" color="#fa0" />阅读历史
            </van-col>
          </van-row>

          <!-- 单元格 -->
          <!-- 编辑cell -->
            <van-cell-group class="user-group">
              <van-cell icon="edit" title="编辑资料" is-link @click="$router.push('/edit')"/>
              <van-cell icon="chat-o" title="小智同学" is-link />
              <van-cell icon="setting-o" title="系统设置" is-link />
              <van-cell icon="warning-o" title="退出登录" is-link 
              @click="logOutClick"
              />
            </van-cell-group>
  </div>
</template>

<script>
import {getUserInfo} from './user.js'
import {removeToken} from '../../utils/token.js'
  export default {
      data() {
        return {
          userInfo:{}
        }
      },
      methods: {
        logOutClick(){
          this.$dialog.confirm({
          title: '来吧,搞起',
          message: '狼人',
        }).then(res=>{
          // 1.成功就清除token , 清除数据 , 跳转首页
            removeToken()
            this.userInfo = {}
            this.$router.push({path:'/home'})
        }).catch(err=>{
          console.log(err);
        })
        }
      },
      // 用第二个生命周期钩子发起请求 , 渲染数据
      created(){
        getUserInfo().then(res=>{
          console.log(res);
          // 使用Dialog弹出框 , 成功then , 失败catch
          // 绑定数据
          this.userInfo = res.data.data
          // 更新vuex数据
          this.$store.commit('setUserInfo',res.data.data)
        })
        
        
      }
}
</script>

<style lang="less">
.container {
  /* 头部 */
  .info-box {
    height: 100px;
    background-color: #3296fa;
    display: flex;
    align-items: center;
    padding-left: 15px;
    .name {
      font-size: 18px;
      margin-left: 15px;
      color: white;
      font-weight: normal;
    }
  }

  /* 操作导航 */
  .user-nav {
    padding: 15px 0;
    font-size: 12px;
    text-align: center;
    background-color: #fff;
    .van-icon {
      display: block;
      font-size: 24px;
      padding-bottom: 5px;
    }
  }
}
</style>